package com.qf.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.dao.UserDao;
import com.qf.entity.User;
import com.qf.service.UserService;
import com.qf.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service //声明bean，且id="userServiceImpl"
@Scope("singleton") //声明创建模式，默认为单例模式 ；@Scope("prototype")即可设置为多例模式
public class userServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public User findUser(Integer id) {
        return userDao.findById(id);
    }


    //service层要开启事物 查询的时候@Tansactional
    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<User> findAll() {
        return userDao.findAllUser();
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public PageInfo<User> findAll(PageVo pageVo) {
        PageHelper.startPage(pageVo.getPage(),pageVo.getLimit());
//        PageHelper.startPage(pageVo.getPageNum(),pageVo.getPageSize());
        List<User> users=userDao.findAllUser();
        return new PageInfo<User>(users);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public PageInfo<User> findAll(PageVo pageVo, User user) {
        PageHelper.startPage(pageVo.getPage(),pageVo.getLimit());
        List<User> users=userDao.findAllUserC(user);
        return new PageInfo<>(users);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer insertUser(User user) {

        return userDao.insertUser(user);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer deleteUser(Integer[] ids) {
        return userDao.delUsers(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updateUser(User user) {
        return userDao.updateUser(user);
    }
}
